﻿using System;
using System.Data;
using System.Collections.Generic;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.DataAccess;
using YiYiTong.Rocket.EFDBFirst;
using System.Web;
using System.Linq;
using YiYiTong.Rocket.Business.Biz;

namespace YiYiTong.Rocket.Business
{
    public class HuodongBiz
    {
        #region 单例模式
        private HuodongBiz()
        {
        }

        private static HuodongBiz _instance;
        private static readonly object _syncObject = new object();

        public static HuodongBiz Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncObject)
                    {
                        if (_instance == null)
                        {
                            _instance = new HuodongBiz();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion 单例模式


        /// <summary>
        /// 活动列表查询
        /// </summary>
        /// <param name="label_type">1 直播 2 预热 3 爆款</param>
        /// <param name="brand_id">品牌id</param>
        /// <param name="category_id">分类id</param>
        /// <param name="selKey">搜索关键字</param>
        /// <returns></returns>
        public Tuple<bool, HuodongListModel> GetList(long uid, int pageIndex, int pageRows)
        {
            DataSet ds = HuodongDataAccess.Instance.GetHuodongList(uid, pageIndex, pageRows);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[1].Rows.Count > 0)
            {
                HuodongListModel model = new HuodongListModel();
                model.list = new ModelHandler<HuodongModel>().FillModel(ds.Tables[1]);
                model.allrows = ConvertHelper.ConvertTo<int>(ds.Tables[0].Rows[0]["allrows"]);
                model.allpages = ConvertHelper.ConvertTo<int>(ds.Tables[0].Rows[0]["allpages"]);

                if (model.list != null && model.list.Any())
                {
                    model.list.ForEach(t =>
                    {
                        t.img_url = SysConfigBiz.Instance.GetThingIMG(t.img_url);
                    });
                }

                return new Tuple<bool, HuodongListModel>(true, model);
            }
            else
                return new Tuple<bool, HuodongListModel>(false, null);
        }

        /// <summary>
        /// 竞价详情查询
        /// </summary>
        /// <returns></returns>
        public Tuple<bool, HuodongModel> GetInfo(long uid, long huodong_id)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ADZ_HuodongMaster aDZ_HuodongMaster = dbcontext.ADZ_HuodongMaster.FirstOrDefault(t => t.huodong_id == huodong_id);
            if (aDZ_HuodongMaster == null)
            {
                return new Tuple<bool, HuodongModel>(false, null);
            }
            HuodongModel huodongModel = new HuodongModel();
            List<string> exp = new List<string>();
            Utils.Mapper(huodongModel, aDZ_HuodongMaster, exp);

            huodongModel.img_url = SysConfigBiz.Instance.GetThingIMG(huodongModel.img_url);

            return new Tuple<bool, HuodongModel>(true, huodongModel);
        }

        public string getShareUrl(long seq_id)
        {
            string base_url = ConfigHelper.AppSetting<string>("huodong_share_url", "");
            string url = $"{base_url}/{seq_id}";
            return url;
        }
        public HuodongShareModel getShareInfo(long seq_id)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            HuodongShareModel model = new HuodongShareModel();
            var aDZ_HuodongShare = dbcontext.ADZ_HuodongShare.FirstOrDefault(x => x.seq_id == seq_id);
            if (aDZ_HuodongShare != null && !string.IsNullOrWhiteSpace(aDZ_HuodongShare.query_params))
            {
                model = JsonHelper.Deserialize<HuodongShareModel>(aDZ_HuodongShare.query_params);
                var huodonginfo = dbcontext.ADZ_HuodongMaster.FirstOrDefault(t => t.huodong_id == aDZ_HuodongShare.huodong_id);
                if (huodonginfo != null)
                {
                    model.imgurl = SysConfigBiz.Instance.GetThingIMG(huodonginfo.img_url, 0, "600");
                    model.huodong_status = huodonginfo.huodong_status;
                }
            }
            model.seq_id = seq_id;
            return model;

        }


        /// <summary>
        /// 商品查询
        /// </summary>
        /// <param name="label_type">1 直播 2 预热 3 爆款</param>
        /// <param name="brand_id">品牌id</param>
        /// <param name="category_id">分类id</param>
        /// <param name="selKey">搜索关键字</param>
        /// <returns></returns>
        public Tuple<bool, GoodsListModel> GetGoodsList(long uid, long huodong_id, int no_store, int pageIndex, int pageRows, int isshare = 0)
        {
            DataSet ds = HuodongDataAccess.Instance.GetGoodsList(uid, huodong_id, no_store, pageIndex, pageRows, isshare);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[1].Rows.Count > 0)
            {
                GoodsListModel model = new GoodsListModel();
                model.list = new ModelHandler<GoodsModel>().FillModel(ds.Tables[1]);
                model.allrows = ConvertHelper.ConvertTo<int>(ds.Tables[0].Rows[0]["allrows"]);
                model.allpages = ConvertHelper.ConvertTo<int>(ds.Tables[0].Rows[0]["allpages"]);

                ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
                var huodong = dbcontext.ADZ_HuodongMaster.FirstOrDefault(t => t.huodong_id == huodong_id);
                if (huodong != null)
                {
                    foreach (var item in model.list)
                    {
                        //处理图片
                        item.imglist = GoodsBiz.Instance.GetGoodsIMG(item.goods_id);
                        item.show_min_level = 0;
                        if(huodong.huodong_status == 0)
                        {
                            item.price = 0;
                        }
                    }
                }

                return new Tuple<bool, GoodsListModel>(true, model);
            }
            else
                return new Tuple<bool, GoodsListModel>(false, null);
        }
    }
}
